#!/bin/bash
# 数组
# characters=("hidden")
characters="lp"


for file in /dev/usb/*;
do
    base_file=$(basename "$file")
    # 取文件名前两个字符
    prefix=${base_file:0:2}
    # 比较文件名前两个字符是否等于指定的字符

    # if [[ "$base_file" != "${characters[0]}" ]]; then
    if [ "$prefix" == "$characters" ]; then
        echo "Opening device: $base_file"
        nL=124  # 用您需要的具体数值替换
        nH=0   # 用您需要的具体数值替换

        # 初始化打印机
        echo -ne '\x1B\x40'  > "$file"
        #左对齐的指令集
        echo -ne '\x1B\x61\x00' > "$file"    
        #设置字体和字体大小
        echo -ne '\x1B\x4D\x00' > "$file"
        echo -ne '\x1D\x21\x01' > "$file"
        #打印抬头
        print_head=${1}
        printf " %s\n" "$print_head" | iconv -f UTF-8 -t GBK >> "$file"

        # 发送 ESC/P 指令
        #中间对齐的指令集
        echo -ne '\x1B\x61\x01' > "$file"
        #设置字体和字体大小
        echo -ne '\x1B\x4D\x00' > "$file"
        echo -ne '\x1D\x21\x01' > "$file"
        # 发送文本内容
        string="测试结果"
        printf "%s\n" "$string" | iconv -f UTF-8 -t GBK >> "$file"
        
        #字体大小
        echo -ne '\x1D\x21\x00' > "$file"
        string3="日差"
        string4="偏振"
        string5="摆幅"
        printf "%4s    %4s   %4s\n"  "$string3" "$string4" "$string5"  |iconv -f UTF-8 -t GBK >> "$file"

        # ./print_dia.sh 表匠校表仪 1 2 3
        printf "%-5.1f   %-4.1f   %-3s\n" "$2" "$3" "$4" >> "$file"


        printf "\n" >> "$file" 
        printf "\n" >> "$file" 
        printf "\n" >> "$file" 
        # Further operations if needed
    fi
done


# 蓝牙USB串口
if [ -e /dev/ttyACM0 ]; then
    owner_stat=$(stat -c '%U' /dev/ttyACM0)
    if [ "$owner_stat" == "bj" ]; then
        echo "The owner of /dev/ttyACM0 is bj (checked with stat)."
    else
        echo "The owner of /dev/ttyACM0 is not bj (checked with stat). It is $owner_stat."
        echo 1 | sudo -S chown bj /dev/ttyACM0
    fi

    file="/dev/ttyACM0"

    # 初始化打印机
    echo -ne '\x1B\x40'  > "$file"
    #左对齐的指令集
    echo -ne '\x1B\x61\x00' > "$file"    
    #设置字体和字体大小
    echo -ne '\x1B\x4D\x00' > "$file"
    echo -ne '\x1D\x21\x01' > "$file"
    #打印抬头
    print_head=${1}
    printf " %s\n" "$print_head" | iconv -f UTF-8 -t GBK >> "$file"

    # 发送 ESC/P 指令
    #中间对齐的指令集
    echo -ne '\x1B\x61\x01' > "$file"
    #设置字体和字体大小
    echo -ne '\x1B\x4D\x00' > "$file"
    echo -ne '\x1D\x21\x01' > "$file"
    # 发送文本内容
    string="测试结果"
    printf "%s\n" "$string" | iconv -f UTF-8 -t GBK >> "$file"
    
    #字体大小
    echo -ne '\x1D\x21\x00' > "$file"
    string3="日差"
    string4="偏振"
    string5="摆幅"
    printf "%4s    %4s   %4s\n"  "$string3" "$string4" "$string5"  |iconv -f UTF-8 -t GBK >> "$file"

    # ./print_dia.sh 表匠校表仪 1 2 3
    printf "%-5.1f   %-4.1f   %-3s\n" "$2" "$3" "$4" >> "$file"


    printf "\n" >> "$file" 
    printf "\n" >> "$file" 
    printf "\n" >> "$file" 


    printf "\n" >> "$file" 
    printf "\n" >> "$file" 
    printf "\n" >> "$file" 


    # Further operations if needed

else
    echo "/dev/ttyACM0 does not exist (checked with test)" 
fi





